//
//  MBForumViewController.m
//  MiBaoTu
//
//  Created by admin on 16/10/14.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBForumViewController.h"
#import "MBForumBaseViewController.h"
#import "MBForumReleaseViewController.h"

#import "MBSpaceMyParentChildShowTabbarView.h"
#import "MBLoginController.h"
@interface MBForumViewController ()<MBSpaceMyParentChildShowTabbarViewDelegate,UIScrollViewDelegate> {
    UIImageView                             *_headerImgView;   //论坛最新活动图片
    MBSpaceMyParentChildShowTabbarView      *_tabbarView;      //分栏
    NSMutableArray                          *_ViewCtrlArr;
    UIScrollView                            *_scrollView;
    NSInteger                               selectTabbarIndex; //分栏选中
}



@end
@implementation MBForumViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNavBar];
    [self onCreateUI];
    [self addController];
}

#pragma mark - UI
- (void)setNavBar
{
    [self setTitleView:@"论坛"];
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
    bt.frame = CGRectMake(0, 0, 60, 30);
    [bt setImage:[UIImage imageNamed:@"fabiao"] forState:UIControlStateNormal];
    [bt setImage:[UIImage imageNamed:@"fabiao"] forState:UIControlStateHighlighted];
    [bt setImageEdgeInsets:UIEdgeInsetsMake(-40, 0, -40, -40)];
    [bt addTarget:self action:@selector(onSelectItemForFaBiao) forControlEvents:UIControlEventTouchUpInside];
    [self setRightBarItems:bt];
}

- (void)onCreateUI
{
    selectTabbarIndex = 0;
    
    //论坛最新获得图片
    _headerImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, (ISIPAD?500*PERCEN:300*PERCEN))];
    _headerImgView.backgroundColor = [UIColor whiteColor];
    [_headerImgView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"banner2"]];
    if (ISIPAD) {
        _headerImgView.image=[UIImage imageNamed:@"banner2I"];
    }
    [self.view addSubview:_headerImgView];
    
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:STORY_BACKGROUND_URL_PATH,@7] view:nil success:^(id json) {
     
        
        if ([json isKindOfClass:[NSDictionary class]]) {
            if ([json[@"ret_code"] integerValue]==0) { 
                NSArray *arr = json[@"data"];
                if (arr.count > 0) {
                    NSDictionary *dic = [arr lastObject];
                    NSString *urlPath = [dic objectForKey:@"PicPath"];
                    NSString *urlPath2 = [dic objectForKey:@"PicPath2"];
                    if (![urlPath isKindOfClass:[NSNull class]]) {
                        if (ISIPAD) {
                            [_headerImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,urlPath2]] placeholderImage:[UIImage imageNamed:@"banner2I"]];
                        }else{
                            
                            
                            [_headerImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,urlPath]] placeholderImage:[UIImage imageNamed:@"banner2"]];
                            
                        }
                      
                    }
                }
                
            }
        }

    } failure:^(NSError *error) {
        
        
    }];
    //歌曲、专辑、亲子秀 分栏
    _ViewCtrlArr = [[NSMutableArray alloc]initWithObjects:@"最新",@"热门",@"活动", nil];
    _tabbarView = [[MBSpaceMyParentChildShowTabbarView alloc]initWithFrame:CGRectMake(0, _headerImgView.bottom, SCREEN_WIDTH,(ISIPAD?100*PERCEN:80*PERCEN)) withCount:_ViewCtrlArr withbgColor:[UIColor blackColor] withSelectColor:TINK_BG_COLOR];
    _tabbarView.delegate = self;
    [self.view addSubview:_tabbarView];

    //全局滚动
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _tabbarView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-_tabbarView.bottom-50)];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(_scrollView.width*_ViewCtrlArr.count, _scrollView.height);
}

//添加子控件
- (void)addController
{
    for (int i=0; i<_ViewCtrlArr.count; i++) {
        MBForumBaseViewController *show = [[MBForumBaseViewController alloc]init];
        if (i==0) {
            show.type = MBForumType_News;
        }
        else if (i==1) {
            show.type = MBForumType_Hots;
        }
        else if (i==2) {
            show.type = MBForumType_Activitys;
        }
        
        __weak typeof(show) selfWeak = show;
        show.onShowUIForBlock = ^ (float offSet) {
            if (offSet >= 0) {
                //隐藏
                _headerImgView.top = NAV_HEIGHT - offSet;
                _tabbarView.top = _headerImgView.bottom;
                _scrollView.top = _tabbarView.bottom;
                _scrollView.height = SCREEN_HEIGHT-_tabbarView.bottom-50;
                selfWeak.tableView.height = SCREEN_HEIGHT-NAV_HEIGHT-(ISIPAD?500*PERCEN:300*PERCEN)-80*PERCEN-50+offSet;
//                NSLog(@"offset:%.2f _headerImgView.top:%.2f _tabbarView.top:%.2f",offSet,_headerImgView.top,_tabbarView.top);
            } else {
                //显示
//                _headerImgView.top = NAV_HEIGHT - (ISIPAD?500*PERCEN:300*PERCEN) - offSet;
//                _tabbarView.top =  _headerImgView.bottom;
//                _scrollView.top = _tabbarView.bottom;
//                _scrollView.height = SCREEN_HEIGHT-_tabbarView.bottom-50;
//                selfWeak.tableView.height = SCREEN_HEIGHT-NAV_HEIGHT-80*PERCEN-50+offSet;
//                selfWeak.tableView.contentOffset = CGPointMake(0, -offSet);
//                NSLog(@"scrollvie.top:%.2f tableView.top:%.2f",_scrollView.top,selfWeak.tableView.top);
//                NSLog(@"offset:%.2f _headerImgView.top:%.2f _tabbarView.top:%.2f",offSet,_headerImgView.top,_tabbarView.top);
            }
        };
        [self addChildViewController:show];

        if (i == 0) {
            show.view.frame = CGRectMake(0, 0, _scrollView.width, _scrollView.height);
            [_scrollView addSubview:show.view];
        }
    }
}

#pragma mark - 发表
- (void)onSelectItemForFaBiao
{
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model) {
        MBLoginController *registerLastVC = [[MBLoginController alloc] init];
        registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
        [self.navigationController pushViewController:registerLastVC animated:YES];
        return;
    }

    MBForumReleaseViewController *release = [[MBForumReleaseViewController alloc]init];
    [self.navigationController pushViewController:release animated:YES];
}

#pragma mark - 最新、热门、活动
- (void)onSelectItemForView:(MBSpaceMyParentChildShowTabbarView *)view withIndex:(NSInteger)selectIndex
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectIndex) {
        selectTabbarIndex = selectIndex;
        [_scrollView setContentOffset:CGPointMake(selectIndex*_scrollView.width, 0) animated:YES];
    }
}

#pragma mark - 滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectTabbarIndex) {
        selectTabbarIndex = index;
        [_tabbarView onUPdateUIForSelectIndex:index];
        
        [self scrollViewDidEndScrollingAnimation:_scrollView];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    MBForumBaseViewController *show = self.childViewControllers[index];
    //UI update
    if (_headerImgView.top > 0 ) {
        //显示
        [show onUPdateUIForTable:SCREEN_HEIGHT-NAV_HEIGHT-(ISIPAD?500*PERCEN:300*PERCEN)-80*PERCEN-50];
    } else {
        //隐藏
        [show onUPdateUIForTable:SCREEN_HEIGHT-NAV_HEIGHT-80*PERCEN-50];
    }
    
    if (show.view.superview) {
        return;
    }
    show.view.frame = scrollView.bounds;
    [_scrollView addSubview:show.view];
}

@end
